package proyecto.BusinessLogic;

import java.util.List;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import proyecto.DataAccessObject.AlumnoDAORemote;
import proyecto.EntityBeans.Alumno;

@Stateless
@Remote(AlumnoBLRemote.class)
public class AlumnoBL implements AlumnoBLRemote {

	AlumnoDAORemote alumnoDAORemote;
	
	public int save(Alumno alumno) {	
		
		try
		{
			Context context = new InitialContext();
			alumnoDAORemote = (AlumnoDAORemote) context.lookup("AlumnoDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
			
		return alumnoDAORemote.save(alumno); //alta de alumno		
		
	}
		
	public Alumno findById(int id) {
		try
		{
			Context context = new InitialContext();
			alumnoDAORemote = (AlumnoDAORemote) context.lookup("AlumnoDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
			
		return alumnoDAORemote.findById(id);
		
	}
	
	
	public List findMaxCodigo() {
		try
		{
			Context context = new InitialContext();
			alumnoDAORemote = (AlumnoDAORemote) context.lookup("AlumnoDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
			
		return alumnoDAORemote.findMaxCodigo();
		
	}
	
		
	public void merge(Alumno alumno) {
		try
		{
			Context context = new InitialContext();
			alumnoDAORemote = (AlumnoDAORemote) context.lookup("AlumnoDAO/remote");
			alumnoDAORemote.merge(alumno);
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}	
		
	}
	
		
	public List findAll() {		
		try
		{
			Context context = new InitialContext();
			alumnoDAORemote = (AlumnoDAORemote) context.lookup("AlumnoDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		return alumnoDAORemote.findAll();
	}
	
	
	public List findAllNoBaja() {		
		try
		{
			Context context = new InitialContext();
			alumnoDAORemote = (AlumnoDAORemote) context.lookup("AlumnoDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		return alumnoDAORemote.findAllNoBaja();
	}
	
	
	public List findAllBaja() {		
		try
		{
			Context context = new InitialContext();
			alumnoDAORemote = (AlumnoDAORemote) context.lookup("AlumnoDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		return alumnoDAORemote.findAllBaja();
	}
	
	
	public List findByIdList(int id)
	{	     
		try
		{
			Context context = new InitialContext();
			alumnoDAORemote = (AlumnoDAORemote) context.lookup("AlumnoDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
	    return alumnoDAORemote.findByIdList(id);
	}
	
		
	public void remove(int idAlumno){	
		try
		{
			Context context = new InitialContext();
			alumnoDAORemote = (AlumnoDAORemote) context.lookup("AlumnoDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		alumnoDAORemote.remove(idAlumno);
	}
	
	public void refresh(Alumno alumno){
		try
		{
			Context context = new InitialContext();
			alumnoDAORemote = (AlumnoDAORemote) context.lookup("AlumnoDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		alumnoDAORemote.refresh(alumno);
	}
}
